//
//  DMCycleImage.m
//  10_HomeWork
//
//  Created by dllo on 16/7/28.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "DMCycleImage.h"
NS_ASSUME_NONNULL_BEGIN
@interface DMCycleImage () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@end
NS_ASSUME_NONNULL_END

@implementation DMCycleImage
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 改变pageControl的对应下标
    NSInteger page = _cycleScrollView.contentOffset.x / self.bounds.size.width;
    _imagesPageControl.currentPage = page - 1;
    if (page == 0) {
        // 改变滚动视图的偏移量
        // _imagesArray是首尾添加后的数组
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_iamgesArray.count - 2), 0);
        _imagesPageControl.currentPage = _iamgesArray.count - 1;
    } else if (page == _iamgesArray.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagesPageControl.currentPage = 0;
    }
}
#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 子控件初始化时(使用下划线形式)frame不要与初始化方法的frame一样, 推荐使用self.bounds或者
        // 注意这里要用bounds
        [self initCycleScrollView];
        [self initImagePageWithFrame:frame];
    }
    return self;
}
- (void)initCycleScrollView {
    self.cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.bounces = NO;
    _cycleScrollView.delegate = self;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_cycleScrollView];
}
- (void)initImagePageWithFrame:(CGRect)frame {
    _imagesPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    _imagesPageControl.center = CGPointMake(frame.size.width / 2, frame.size.height - _imagesPageControl.frame.size.height / 2);
    [_imagesPageControl addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_imagesPageControl];
}
- (void)imagePageChange:(UIPageControl *)pageControl {
    [_cycleScrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}
#pragma mark - ImagesArray
// array存储图片的名字或者url
- (void)setImagesWithArray:(NSArray *)array {
    // 根据传过来的数组, 进行处理, 生成新的图片数组
    NSArray *imageArray = [self handleArray:array];
    // 逻辑判断时需要用到图片数组内容
    self.iamgesArray = [NSArray arrayWithArray:imageArray];
    // 根据数组生成对应的imageView
    [self createAllImageViewsWithImagesArray:imageArray];
    _imagesPageControl.numberOfPages = array.count;
    _cycleScrollView.contentSize = CGSizeMake(imageArray.count * self.bounds.size.width, 0);
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
}
- (void)createAllImageViewsWithImagesArray:(NSArray *)imagesArray {
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}
- (NSArray *)handleArray:(NSArray *)array {
    if (!array) {
        return nil;
    }
    // 最终的图片数组
    NSMutableArray *imagesArray = [NSMutableArray array];
    id firstObject = [array firstObject];
    // 判断对象是不是某种类型isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串数组转换成图片数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]];
            [imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        [imagesArray setArray:array];
    }
    UIImage *firsImage = [imagesArray firstObject];
    UIImage *lastImage = [imagesArray lastObject];
    [imagesArray insertObject:lastImage atIndex:0];
    [imagesArray addObject:firsImage];
    // 把可变数组copy, 得到不可变的数组
    return [imagesArray copy];
}


@end
